<?php
namespace jy_jinxiaofee;
defined('BY_JYA') or exit('error');
/*
	分佣插件工具类
*/
class model extends \plugin{
	
	public function create_condition($conditions){
		if(!$conditions){
			return '';
		}
		if(!is_array($conditions)){
			$conditions=json_decode($conditions,true);
		}
		$level_up_types=$this->level_up_types;
		$current_level_up_types=$this->current_level_up_types();
		$str='';
		foreach($current_level_up_types as $key){
			$str.=$level_up_types[$key].'达'.$conditions[$key].';';
		}
		return $str;
	}
	public function current_level_up_types(){
		return str_array($this->setting['level_up_types']);
	}
	public function up_del_day(){
		$del_day=intval($this->setting['del_day'])?:0;
		if(!$del_day){
			return false;
		}
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$enddaytime=$daytime-$del_day*24*3600;
		$lastdaytime=$enddaytime-$del_day*24*3600;
		$members = pdo_getall('core_members',array('uniacid'=>$_SESSION['uniacid'],'credit2'=>0,'is_virtual'=>0,'mobile <>'=>'','createtime between'=>array($lastdaytime,$enddaytime)),array('id'));
		foreach($members as $lv){
			$ispay=pdo_count('jy_jinxiaofee_order',array('mid'=>$lv['id'],'uniacid'=>$_SESSION['uniacid']));
			if(!$ispay){
				pdo_delete('core_members',array('id'=>$lv['id'],'uniacid'=>$_SESSION['uniacid']));
				pdo_delete('jy_jinxiaofee_member',array('mid'=>$lv['id'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
		return false;
	}
	public function gored($city=''){
		if(!$this->setting['is_redopen']){
			return $red;
		}
		$pindex = max(1, intval($_RQ['page']));
		$psize = 10;
		$total='';
		$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>array(6,7),'status'=>4);
		$logs=pdo_getslice('jy_jinxiaofee_commission',$params,array($pindex,$psize),$total,$fields,'','id desc');
		$tips='';
		foreach($logs as $lv){
			$nickname=pdo_getcolumn('core_members',array('id'=>$lv['mid'],'uniacid'=>$_SESSION['uniacid']), 'nickname');
			$time=date('Y-m-d H:i:s',$lv['complete_time']);
			$tips .=$nickname.'[领取了红包],时间:'.$time.']；';
		}
		$red['tips']=$tips;
		$red['getrednum']=pdo_count('jy_jinxiaofee_commission',array('type'=>array(6,7),'status'=>4,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		$red['mygetallfee']=pdo_getsum('jy_jinxiaofee_commission',array('type'=>array(6,7),'status'=>4,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'money');
		$red['lastrednum']=pdo_count('jy_jinxiaofee_commission',array('type'=>array(6,7),'status'=>3,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		$red['lastrednum']=$red['lastrednum']?:0;
		$red['lastfee']=pdo_getsum('jy_jinxiaofee_commission',array('type'=>array(6,7),'status'=>3,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'money');
		return $red;
	}
	public function get_area_code($area){
		if(!$area['province']){
			return '';
		}
		$address=$area['province'];
		if($area['city']){
			$address.=' '.$area['city'];
		}
		if($area['district']){
			$address.=' '.$area['district'];
		}
		if($area['street']){
			$address.=' '.$area['street'];
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
		if(!$area_code){
			$isup=pdo_count('core_area',array(),'code');
			if($isup==0){
				require ROOT_D.'/update/update_area.php';
			}
		}
		return $area_code;
	}
}